
package com.tools.cleanmaster.ui.view.trash;

import android.graphics.drawable.Drawable;
import android.text.Html;

import com.tools.cleanmaster.R;
import com.tools.cleanmaster.model.TrashType;
import com.tools.cleanmaster.model.item.ApkFileItem;
import com.tools.cleanmaster.model.item.TrashItem;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

@SuppressWarnings("static-access")
public class TrashApkGroup extends TrashGroup {

    public TrashApkGroup() {
        super(null);
        mIsCheckedDefault = false;
    }

    public class ApkTrash extends TrashViewItemSingle {
        public ApkFileItem mApkItem;
        private final String mDownloadTime;

        ApkTrash(TrashItem item) {
            super(item, TrashApkGroup.this);
            if (mTrashItem instanceof ApkFileItem) {
                mApkItem = (ApkFileItem) item;
                mDownloadTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date(
                        mApkItem.apkCreateTime));
            } else {
                throw new IllegalArgumentException("Not ApkFileItem found in TrashApkGroup");
            }
        }

        @Override
        public Drawable getIcon() {
            Drawable icon = null;
            if (icon != null) {
                return icon;
            } else {
                // for Broken apk file
                return mContext.getResources().getDrawable(R.drawable.transhcleaning_apktrash);
            }
        }

        @Override
        public String getTitle() {
            return mApkItem.hasFlag(ApkFileItem.FLAG_BROKEN) ? mContext.getResources().getString(
                    R.string.trash_clean_broken_apk) : mApkItem.appName;
        }

        public CharSequence getMessage() {
            return Html.fromHtml(mContext.getString(R.string.trash_clean_apk_dialog_content,
                    getStatus(), getVersion(), mDownloadTime, mApkItem.filePath));
        }

        public String getVersion() {
            return mApkItem.hasFlag(ApkFileItem.FLAG_BROKEN) ? mContext
                    .getString(R.string.trash_clean_apk_version_broken) : mApkItem.versionName;
        }

        public String getInstalledDesc() {
            if (mApkItem.hasFlag(ApkFileItem.FLAG_BROKEN)) {
                return "";
            } else if (mApkItem.hasFlag(ApkFileItem.FLAG_INSTALLED)) {
                return mContext.getString(R.string.trash_clean_apk_installed);
            } else {
                return mContext.getString(R.string.trash_clean_apk_uninstalled);
            }
        }
    }

    @Override
    public Drawable getIcon() {
        return mContext.getResources().getDrawable(R.drawable.icon_trash_clean_useless_apks);
    }

    @Override
    public String getTitle() {
        return mContext.getResources().getString(R.string.useless_apk_trash_item);
    }

    @Override
    public void fillTrashes(Map<TrashType, List<TrashItem>> trashes) {
        List<TrashItem> list = trashes.get(TrashType.APK_FILE);
        if (list != null && !list.isEmpty()) {
            for (TrashItem trashItem : list) {
                addChildItem(new ApkTrash(trashItem));
            }
            // 设置check状态
            setCheckStateByChild();
            Collections.sort(mChildItems);
        }
    }

}
